
package hz.mobileproject.pda.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import hz.mobileproject.pda.R;
import hz.mobileproject.pda.entity.response.AllTalkResPonse;
import hz.mobileproject.pda.listener.Send_end_Callroll_inter;


/**
 * Created by YOLANDA on 2016/7/22.
 */
public class AllTalkListAdapter extends  RecyclerView.Adapter<AllTalkListAdapter.ViewHolder> {


    private final Context context;
    public List<AllTalkResPonse> datas;
    private Send_end_Callroll_inter callroll_inter;
    private Boolean onOff;

    //上一个播放的position
    private int lastPlayPosition = -1;



    public AllTalkListAdapter(Context context, List<AllTalkResPonse> noneTalkListBeans) {
        this.context = context;
        if(noneTalkListBeans == null){
            noneTalkListBeans = new ArrayList<AllTalkResPonse>();
        }
        this.datas = noneTalkListBeans;
    }

    //创建新View，被LayoutManager所调用
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_person_all_talk,viewGroup,false);
        ViewHolder vh = new ViewHolder(view);
        return vh;
    }
    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
//        viewHolder.mTextView.setText(datas[position]);
        holder.police_name.setText(String.valueOf(datas.get(position).police_name));
        holder.prisoner_name.setText(String.valueOf(datas.get(position).prisoner_name));
        holder.dprt_name.setText(String.valueOf(datas.get(position).dprt_name));
        holder.talk_class.setText(String.valueOf(datas.get(position).talk_classify));
        holder.talk_time.setText(String.valueOf(datas.get(position).talk_time_long));
        //防止复用产生的混乱
        if(datas.get(position).isPlaying) {
            holder._play.setVisibility(View.GONE);
            holder._showPauseAndStop.setVisibility(View.VISIBLE);
        }else {
            holder._play.setVisibility(View.VISIBLE);
            holder._showPauseAndStop.setVisibility(View.GONE);
        }
        holder._play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(lastPlayPosition >= 0) {
                    datas.get(lastPlayPosition).isPlaying = false;
                    notifyDataSetChanged();
                }
                lastPlayPosition = position;
                //设置是否播放为true
                datas.get(position).isPlaying = true;
                if(onBtnClick != null) {
                    holder._showPauseAndStop.setVisibility(View.VISIBLE);
                    holder._play.setVisibility(View.GONE);
                    onBtnClick.playRecord(datas.get(position).audio_path);
                }
            }
        });
        //暂停
        holder._pause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if("继续".equals(holder._pause.getText().toString())) {
                    //当按钮 为继续的时候点击的逻辑
                    holder._pause.setText("暂停");
                    if(onBtnClick != null) {
                        //继续播放
                        onBtnClick.reStartPlayRecord();
                        return;
                    }
                }else if("暂停".equals(holder._pause.getText().toString())) {
                    holder._pause.setText("继续");
                    if(onBtnClick != null) {
                        //暂停
                        onBtnClick.pausePlayRecord();
                    }
                }
            }
        });
        //停止
        holder._stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                lastPlayPosition = -1;
                //设置是否播放为false
                datas.get(position).isPlaying = false;
                if(onBtnClick != null) {
                    //停止
                    holder._showPauseAndStop.setVisibility(View.GONE);
                    holder._play.setVisibility(View.VISIBLE);
                    onBtnClick.stopPlayRecord();
                }
            }
        });


    }
    //获取数据的数量
    @Override
    public int getItemCount() {

//        LogUtils.e("datas.size():"+datas.size()+onOff);

        if(datas == null) {
            return 0;
        }else {

            return datas.size();
        }

    }

    public void setData(List<AllTalkResPonse> allTalkResPonses) {
        datas.clear();
        if(allTalkResPonses == null) {
            allTalkResPonses = new ArrayList<>();
        }
        datas.addAll(allTalkResPonses);
        notifyDataSetChanged();
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView police_name;
        public TextView prisoner_name;
        public TextView dprt_name;
        public TextView talk_class;   //时间, 现在改成监区
        public TextView talk_time;

        private Button _play;
        private Button _pause;
        private Button _stop;
        private LinearLayout _showPauseAndStop;
        public ViewHolder(View view){
            super(view);
            police_name = (TextView) view.findViewById(R.id.tv_police_name);
            prisoner_name = (TextView) view.findViewById(R.id.tv_NAME);
            dprt_name = (TextView) view.findViewById(R.id.dprt_name);
            talk_class= (TextView) view.findViewById(R.id.talk_class);
            talk_time = (TextView) view.findViewById(R.id.talk_time);
            _play = (Button) view.findViewById(R.id.btn_play);
            _pause = (Button) view.findViewById(R.id.btn_pause);
            _stop = (Button) view.findViewById(R.id.btn_stop);
            _showPauseAndStop = (LinearLayout) view.findViewById(R.id.ly_pause_stop);
        }
    }

//    public void removeItem(Response_loadPrisonerList model) {
//        int position = datas.indexOf(model);
//        LogUtils.e("position():"+position+"");
//        /*if (position!=0){
//            datas.remove(position);
//            notifyItemRemoved(position);//Attention!
//        }*/
//
//        for (int i=0;i<datas.size();i++){
//            if (model.getPrisoner_id().equals(datas.get(i).getPrisoner_id())){
//                datas.remove(i);
//                notifyItemRemoved(i);//Attention!
//                break;
//            }
//        }
//
//    }




    public void addData(List<AllTalkResPonse> datass) {
        datas.addAll(datass);
        notifyDataSetChanged();
    }



    private OnBtnClick onBtnClick;

    public interface OnBtnClick{
        void playRecord(String url);
        void pausePlayRecord();
        void stopPlayRecord();
        void reStartPlayRecord();
    }

    public void setOnPlayBtnClick(OnBtnClick onBtnClick){
        this.onBtnClick = onBtnClick;
    }
}
